/* -----------------------------------------------------------------------------
 * PROJECT    : net.lwf
 * FILE       : fMagicAlphabetHandler.java
 *
 * HISTORY:
 * Date          Author                        Description
 * ----------------------------------------------------------------------------- 
 * Jan 11, 2009       Raghu Sarraff			       Created
 */
package lwf.handler.english.word;

import java.util.List;

import net.lwf.handler.util.ImageHtml;

import lwf.handler.english.WordHandler;


public class AlphabetDictionaryHandler extends WordHandler {
	
	private String alphabet;
	private String info;
	
	public AlphabetDictionaryHandler(int start, int end, String _alphabet, List data) {
		this.alphabet = _alphabet;
		int size = 0;
		if(data != null) {
			size = data.size();
		}
		this.info = "There are " + size + " dictionary words available for the alphabet '" + _alphabet + "'";
		setShowbuttontop(true);
		setDisplayData(start, end, data);
	}
	
	//----------------------------------------------------------------------------------------------
	// Overridden methods
	//----------------------------------------------------------------------------------------------
	public String getFormattedDisplay() {
		String data = super.getFormattedDisplay();
		StringBuffer sbAlphabet = new StringBuffer();
		sbAlphabet.append("<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");
			sbAlphabet.append("<tr>");
			    sbAlphabet.append("<td width=\"20%\" class=\"alphabetbig\" >");
		        	sbAlphabet.append(alphabet.toUpperCase() + "&nbsp;" + alphabet.toLowerCase());
		        sbAlphabet.append("</td>");
			    sbAlphabet.append("<td width=\"80%\" class=\"tablesubheader\" >");
	        		sbAlphabet.append(this.info);
	        	sbAlphabet.append("</td>");
			sbAlphabet.append("</tr>");
			sbAlphabet.append("<tr>");
			    sbAlphabet.append("<td colspan=\"2\" >");
			        sbAlphabet.append(super.getFormattedDisplay());
			    sbAlphabet.append("</td>");
			sbAlphabet.append("</tr>");
	    sbAlphabet.append("</table>");
	    return sbAlphabet.toString();
	}
	
	protected String processRowData(Object obj) {
		if (obj != null) {
			return this.getWordData((String) obj);
		}
		return "";
	} //processRowData
	
	//----------------------------------------------------------------------------------------------
	// Customized methods
	//----------------------------------------------------------------------------------------------
	public static String getAlphabetKeys() {
		    String[] alphabets = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
		    
			StringBuffer sbAlpabets = new StringBuffer();

			sbAlpabets.append("<table width=\"91%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");
			boolean countdisplay = false;
			int nCounter = 0;
			int nDataCount = 0;
	        while(!countdisplay) {
	        	sbAlpabets.append("<tr>");
		        	for(int i=0; i<13; i++) {
		        		String value = "";	
		        		sbAlpabets.append("<td class=\"data\" width=\"5%\">");
		        		    String alphabet = "&nbsp;";
		        			if(nDataCount<alphabets.length && !countdisplay ) {
		        				alphabet = (String) alphabets[nDataCount];
		        				String onclick = "processURL('" + alphabet + "')";
		        		    	sbAlpabets.append("<a href=\"#\" onclick=\"" + onclick + "\" >");
		        		    	sbAlpabets.append(alphabet);
		        		    	sbAlpabets.append("</a>");
								nDataCount++;
		        		    }
		        		    else {
		        		    	sbAlpabets.append("&nbsp;");
		        		    	countdisplay = true;
		        		    }
		        		sbAlpabets.append("</td>");
		        		sbAlpabets.append("<td width=\"2%\" >");
		        			sbAlpabets.append("&nbsp;");
		        		sbAlpabets.append("</td>");
		        	}
		        	nCounter = nCounter + 13;
	        	sbAlpabets.append("</tr>");
	        }
	        sbAlpabets.append("</table>");      			

	        return sbAlpabets.toString();
	}	
	
	
	//----------------------------------------------------------------------------------------------
	// Private methods
	//----------------------------------------------------------------------------------------------
	
    private String getWordData(String word) {
    	String image = ImageHtml.getWordImage(word);
		StringBuffer sbWData = new StringBuffer();
		sbWData.append("<table   width=\"100%\" border=\"0\" align=\"center\" cellspacing=\"0\" cellpadding=\"0\">");
			sbWData.append("<tr>");
			    if(image.length() != 0) {
//			    		sbWData.append("<td  width=\"38%\">");
//							sbWData.append(image);
//					    sbWData.append("</td>");
//					    sbWData.append("<td width=\"5%\" > &nbsp;</td>");
//					    sbWData.append("<td width=\"2%\" class=\"alphabetmedium\"> " + this.alphabet + "</td>");
//					    sbWData.append("<td width=\"10%\" class=\"data\"> for </td>");
//					    sbWData.append("<td width=\"10%\" class=\"data\"> ---> </td>");
//					    sbWData.append("<td width=\"40%\" class=\"data\">");
//					    	sbWData.append(word);
//				        sbWData.append("</td>");
					    sbWData.append("<td width=\"2%\" class=\"alphabetmedium\"> " + this.alphabet + "</td>");
					    sbWData.append("<td width=\"10%\" class=\"data\"> for </td>");
					    sbWData.append("<td width=\"10%\" class=\"data\"> ---> </td>");
					    sbWData.append("<td width=\"40%\" class=\"data\">");
					    	sbWData.append(word);
				        sbWData.append("</td>");			    	
				        sbWData.append("<td width=\"5%\" > &nbsp;</td>");
				        sbWData.append("<td  width=\"38%\">");
							sbWData.append(image);
						sbWData.append("</td>");
			    }
			    else {
					sbWData.append("<td class=\"dataleft\" colspan=\"6\" >");
						sbWData.append(word);
					sbWData.append("</td>");
			    }
			sbWData.append("</tr>");
		sbWData.append("</table>");

		return sbWData.toString();
    }

}

